import os
import sys
sys.path.append('../backend')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'HuaXianGuan.settings')

import django
django.setup()

from apps.flowers.models import FlowerImage

def fix_local_image_paths():
    """修复本地图片路径，替换为OSS链接"""
    
    # 图片路径映射
    image_mapping = {
        '/images/flowers/红郁金香花束.jpg': 'https://flower-1.oss-cn-shanghai.aliyuncs.com/flowers/红郁金香花束.jpg',
        '/images/flowers/roses_1.jpg': 'https://flower-1.oss-cn-shanghai.aliyuncs.com/flowers/红玫瑰花束.jpg',
        '/images/flowers/roses_2.jpg': 'https://flower-1.oss-cn-shanghai.aliyuncs.com/flowers/粉玫瑰花束.jpg',
        '/images/flowers/roses_3.jpg': 'https://flower-1.oss-cn-shanghai.aliyuncs.com/flowers/白玫瑰花束.jpg',
        '/images/flowers/红玫瑰.jpg': 'https://flower-1.oss-cn-shanghai.aliyuncs.com/flowers/红玫瑰花束.jpg',
        '/images/flowers/粉玫瑰.jpg': 'https://flower-1.oss-cn-shanghai.aliyuncs.com/flowers/粉玫瑰花束.jpg',
        '/images/flowers/白玫瑰.jpg': 'https://flower-1.oss-cn-shanghai.aliyuncs.com/flowers/白玫瑰花束.jpg',
        '/images/flowers/红康乃馨.jpg': 'https://flower-1.oss-cn-shanghai.aliyuncs.com/flowers/红康乃馨花束.jpg',
        '/images/flowers/粉康乃馨.jpg': 'https://flower-1.oss-cn-shanghai.aliyuncs.com/flowers/粉康乃馨花束.jpg',
        '/images/flowers/白百合.jpg': 'https://flower-1.oss-cn-shanghai.aliyuncs.com/flowers/白百合花束.jpg',
        '/images/flowers/粉百合.jpg': 'https://flower-1.oss-cn-shanghai.aliyuncs.com/flowers/粉百合花束.jpg',
        '/images/flowers/向日葵.jpg': 'https://flower-1.oss-cn-shanghai.aliyuncs.com/flowers/向日葵花束.jpg',
        '/images/flowers/满天星.jpg': 'https://flower-1.oss-cn-shanghai.aliyuncs.com/flowers/满天星花束.jpg',
        '/images/flowers/粉郁金香.jpg': 'https://flower-1.oss-cn-shanghai.aliyuncs.com/flowers/粉郁金香花束_1.jpg'
    }
    
    # 查找所有使用本地路径的图片
    local_images = FlowerImage.objects.filter(image_url__startswith='/images/')
    
    print(f"找到 {local_images.count()} 个使用本地路径的图片")
    
    for img in local_images:
        old_url = img.image_url
        
        # 查找对应的OSS URL
        new_url = image_mapping.get(old_url)
        
        if new_url:
            img.image_url = new_url
            img.save()
            print(f"✓ 已更新 {img.flower.name}: {old_url} -> {new_url}")
        else:
            # 如果没有找到映射，尝试根据花卉名称生成
            flower_name = img.flower.name
            new_url = f"https://flower-1.oss-cn-shanghai.aliyuncs.com/flowers/{flower_name}.jpg"
            img.image_url = new_url
            img.save()
            print(f"✓ 已生成 {img.flower.name}: {old_url} -> {new_url}")
    
    # 验证修复结果
    remaining_local = FlowerImage.objects.filter(image_url__startswith='/images/')
    if remaining_local.exists():
        print(f"\n⚠️ 还有 {remaining_local.count()} 个本地路径未修复:")
        for img in remaining_local:
            print(f"  {img.flower.name}: {img.image_url}")
    else:
        print("\n✅ 所有本地路径已修复完成!")
    
    # 显示最终结果
    print("\n最终图片列表:")
    all_images = FlowerImage.objects.all()
    for img in all_images:
        print(f"{img.flower.name}: {img.image_url}")

if __name__ == "__main__":
    fix_local_image_paths()